package com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.entity.EmergencyReport;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.entity.Lifeline;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.entity.ListVO;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.entity.Tree;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.param.LifelineParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.service.LifelineService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.vo.LifelineVO;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.vo.ResultVo;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/30
 * @Description: 生命线工程数据查看
 */
@RestController
@RequestMapping("/lifeline")
@Slf4j
@Api(tags = "生命线工程数据查看")
public class LifelineController {
    @Resource
    LifelineService lifelineService;

    /**
     * 分页查询
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(LifelineParam queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=lifelineService.getPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @Operation(summary = "导出数据")
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, @RequestBody LifelineParam param) {
        lifelineService.exportData(response, param);
    }

    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            LifelineVO data = lifelineService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @Operation(summary = "查询生命线类型")
    @GetMapping("/queryLifelineType")
    public RestResponse queryLifelineType(){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            List<Tree> tree=lifelineService.queryLifelineType();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(tree);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @PostMapping("/batchAudit")
    @Operation(summary = "批量审核")
    public RestResponse batchAudit(@RequestBody ListVO list) {
        try {
            lifelineService.batchAudit(list.getList());
            return RestResponse.succeed("成功");
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 修改单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody Lifeline param) {
        try {
            lifelineService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 导入excel
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse uploadSampling(@RequestParam("file") MultipartFile file, ResultVo resultVo) {
        RestResponse response = null;
        try{
            Map<String, Object> map = lifelineService.importExcel(file,resultVo);
            boolean res = ((Boolean) map.get("res")).booleanValue();;
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 新增单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "新增数据")
    public RestResponse save(@RequestBody Lifeline param) {
        try {
            String  id=lifelineService.save(param);
            return RestResponse.succeed(id);
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    /**
     * 批量提交
     * @param ids 数据id
     * @return
     */
    @GetMapping("submit")
    @Operation(summary = "批量提交")
    public RestResponse submit(String ids) {
        try {
            String[] split = ids.split(",");
            List<String> list = new ArrayList<>();
            for (String s : split) {
                list.add(s);
            }
            lifelineService.submit(list);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败!", e);
            return RestResponse.fail("提交失败!");
        }
    }
    /**
     * 批量删除
     * @param ids 数据id
     * @return
     */
    @GetMapping("batchDelete")
    @Operation(summary = "批量删除")
    public RestResponse batchDelete(String ids) {
        try {
            String[] split = ids.split(",");
            List<String> list = new ArrayList<>();
            for (String s : split) {
                list.add(s);
            }
            lifelineService.batchDelete(list);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败!", e);
            return RestResponse.fail("删除失败!");
        }
    }

    /**
     * 生命线工程编号
     * @param eventCode
     * @return
     */
    @GetMapping("/getLifeLineCode")
    public RestResponse getLifeLineCode(String eventCode){
        RestResponse response = null;

        try{
            String str=lifelineService.getLifeLineCode(eventCode);
            response = RestResponse.succeed(str);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
